package com.mvvm.ktnet.retention

import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.mvvm.ktnet.application.App
import com.mvvm.ktnet.base.activity.IBaseVmActivity
import com.mvvm.ktnet.base.fragment.IBaseVmFragment
import com.mvvm.ktnet.base.viewmodel.BaseViewModel

/**
 *
 * @ProjectName:    KtNet
 * @Package:        com.mvvm.ktnet.retention
 * @ClassName:      ViewModelProviders
 * @Description:     java类作用描述
 * @Author:         Alex
 * @CreateDate:     2020/11/16 9:19
 * @UpdateUser:     更新者：
 * @UpdateDate:     2020/11/16 9:19
 * @UpdateRemark:   更新说明：
 * @Version:        1.0
 */

fun <P : BaseViewModel> IBaseVmActivity<*>.ViewModelProviders() {
    var mClass: Class<*>? = this.javaClass
    var codeInt:Int=0
    for (field in mClass!!.declaredFields) {
        //获取字段上的注解
        val anns = field.declaredAnnotations
        if (anns.isEmpty()) {
            continue
        }

        when (anns[0]) {
            is ViewModelActivity -> {
                try {
                    val canonicalName = field.type as Class<ViewModel>
                    var baseViewModel: P =
                        ViewModelProvider(this).get( (++codeInt).toString(), canonicalName) as P
                    field.isAccessible = true
                    field.set(this, baseViewModel)
                    this.addLoadingObserve(baseViewModel)
                } catch (throwable: Throwable) {
                    throwable.printStackTrace()
                }
            }
            is ViewModelApp -> {
                try {
                    val canonicalName = field.type as Class<ViewModel>
                    var baseViewModel: P =
                        App.context.getAppViewModelProvider().get(canonicalName) as P
                    field.isAccessible = true
                    field.set(this, baseViewModel)
                    this.addLoadingObserve(baseViewModel)
                } catch (throwable: Throwable) {
                    throwable.printStackTrace()
                }
            }
        }
    }

}


fun <P : BaseViewModel> IBaseVmFragment<*>.ViewModelProviders() {
    var mClass: Class<*>? = this.javaClass
    var codeInt:Int=0
    for (field in mClass!!.declaredFields) {
        //获取字段上的注解
        val anns = field.declaredAnnotations
        if (anns.isEmpty()) {
            continue
        }
        when (anns[0]) {
            is ViewModelActivity -> {
                try {
                    val canonicalName = field.type as Class<ViewModel>
                    var baseViewModel: P =
                        ViewModelProvider(requireActivity()).get((++codeInt).toString(),canonicalName) as P
                    field.isAccessible = true
                    field.set(this, baseViewModel)
                    this.addLoadingObserve(baseViewModel)
                } catch (throwable: Throwable) {
                    throwable.printStackTrace()
                }
            }
            is ViewModelApp -> {
                try {
                    val canonicalName = field.type as Class<ViewModel>
                    var baseViewModel: P =
                        App.context.getAppViewModelProvider().get(canonicalName) as P
                    field.isAccessible = true
                    field.set(this, baseViewModel)
                    this.addLoadingObserve(baseViewModel)
                } catch (throwable: Throwable) {
                    throwable.printStackTrace()
                }
            }
            is ViewModelFragment -> {
                try {
                    val canonicalName = field.type as Class<ViewModel>
                    var baseViewModel: P = ViewModelProvider(this).get((++codeInt).toString(),canonicalName) as P
                    field.isAccessible = true
                    field.set(this, baseViewModel)
                    this.addLoadingObserve(baseViewModel)
                } catch (throwable: Throwable) {
                    throwable.printStackTrace()
                }
            }
        }
    }

}


